package com.car.controller.maintain;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.dao.user.UserDao;
import com.car.entity.Maintain;
import com.car.entity.User;
import com.car.service.impl.maintain.MaintainServiceImpl;
import com.car.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MaintainController {

    @Autowired
    MaintainServiceImpl maintainServiceImpl;

    @Autowired
    UserDao userDao;

    @RequestMapping("/toMaintainList")
    public ModelAndView toMaintainList() {
        return new ModelAndView("maintain-list");
    }

    @ResponseBody
    @RequestMapping("/maintainList")
    public Map maintainList(String carIdCard, String maintainStart, String maintainEnd, String PageNum, String PageSize) {
        Integer pageNum = Integer.valueOf(PageNum);
        Integer pageSize = Integer.valueOf(PageSize);

        Map<String, Object> map = new HashMap<>();
        map.put("carIdCard", carIdCard);
        map.put("maintainStart", maintainStart);
        map.put("maintainEnd", maintainEnd);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);

        Page page = maintainServiceImpl.selectLikeMaintain(map);
        map.put("page", page);
        map.put("totalPage", page.getTotal() / page.getSize() == 0 ?
                page.getTotal() / page.getSize() :
                (page.getTotal() / page.getSize() + 1)
        );
        return map;
    }

    @ResponseBody
    @RequestMapping("/toAddMaintain")
    public ModelAndView toAddMaintain() {
        return new ModelAndView("maintain-add");
    }

    @ResponseBody
    @RequestMapping("/addMaintain")
    public ResultMsg addMaintain(Maintain maintain, String userName) {
        ResultMsg resultMsg = maintainServiceImpl.addMaintain(maintain, userName);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/delMaintain")
    public ResultMsg delMaintain(Maintain maintain) {
        ResultMsg resultMsg = maintainServiceImpl.delMaintain(maintain);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/updateMaintain")
    public ResultMsg updateMaintain(Maintain maintain, String userName) {
        ResultMsg resultMsg = maintainServiceImpl.updateMaintain(maintain);
        return resultMsg;
    }

    @ResponseBody
    @RequestMapping("/toUpdateMaintain")
    public ModelAndView toUpdateMaintain(Maintain insurance) {
        ModelAndView mv = new ModelAndView();
        ResultMsg resultMsg = maintainServiceImpl.selectById(insurance);
        mv.setViewName("maintain-edit");
        mv.addObject("result", resultMsg);
        return mv;
    }


}
